<?php
  $frm = $_POST;
  //$ses = $_SESSION;
  
  //check if username exists
  if(isset($frm['send']))
  {
    if($frm['send'] == 'Login'){
      $username_input = $frm['username'];
      $password = md5($frm['password']);
      
      $log_info_fetch_query = mysql_query('SELECT gebruikers_naam, wachtwoord FROM USERNAME WHERE BINARY gebruikers_naam = "' . $username_input . '"') or die(mysql_error());
      $status = mysql_query('SELECT status FROM USERNAME WHERE gebruikers_naam = "' . $username_input . '"') or die(mysql_error());
     
      
      if(mysql_num_rows($log_info_fetch_query) <1){
      	echo '<script type="text/javascript">alert("Voer iets in");</script>';
      }
      else{
      	 $_SESSION['status'] = mysql_result($status, 0);
      }
      
      if(isset($_SESSION['status']) && $_SESSION['status'] == 'act')
	  {
	      while($log_row = mysql_fetch_array($log_info_fetch_query))
	      {
	         if($username_input == $log_row['gebruikers_naam'])
	          {
	           
	            $username = $log_row['gebruikers_naam'];
	            $user_first_name_query = mysql_query('SELECT USER_DATA.voornaam, USER_DATA.USERNAME_idUSERNAME FROM USER_DATA, USERNAME WHERE USERNAME.gebruikers_naam = "' . $username . '" AND USERNAME.idUSERNAME = USER_DATA.USERNAME_idUSERNAME') or die(mysql_error());
	          	while($row = mysql_fetch_array($user_first_name_query))
	          	{
	          		$_SESSION['real_name'] = $row['voornaam'];
	          		$_SESSION['userID'] = $row['USERNAME_idUSERNAME'];
	          	}
	          }
	          if($password == $log_row['wachtwoord'])
	          {
	             if(isset($ses['logged']))
	             {
	               $_SESSION['logged'] = true;
	               updateLogboek();
	             }
	             else
	             {
	               $_SESSION['logged'] = true;
	               updateLogboek();
	             }
	             if(isset($ses['username']))
	             {
	                $_SESSION['username'];
	             }
	             else
	             {
	                $_SESSION['username'] = $username;
	             }
	             if(isset($ses['password']))
	             {
	                $_SESSION['password'];
	             }
	             else
	             {
	                $_SESSION['password'] = $password;
	             }
	          }
	          else
	          {
	          	echo '<script type="text/javascript">alert("Het ingevoerde wachtwoord is onjuist");</script>';
	          }
	      }
	    }
	}
	if(isset($_SESSION['status']) && $_SESSION['status'] == 'inact')
    {
    	echo '<script type="text/javascript">alert("Uw account is bij ons gedeactiveerd");</script>';
    }
  }
  if(isset($frm['logout']))
  {
    if($frm['logout'] == 'Uitloggen')
    {
      unset($_SESSION);
      session_destroy();
    }
  }

  function updateLogboek(){
  	$dateNow = date('Y-m-d');
  	$result = mysql_query('UPDATE logboek SET countAccount = countAccount + 1 WHERE datum = "' . $dateNow . '"') or die(mysql_error());
  	
  	if(mysql_affected_rows() == 0)
  	{
  		$result = mysql_query('INSERT INTO logboek (datum, countAccount, weeknr) VALUES ("' . $dateNow . '", 1, ' . date('W') . ')') or die(mysql_error());
  	}
  }
?>
<!DOCTYPE html>
<html>
	<head>
		<link href="style/header.css" rel="stylesheet" type="text/css">
		<link href="style/menu.css" rel="stylesheet" type="text/css">
		<link href="style/slideshow.css" rel="stylesheet" type="text/css">
		<link href="style/footer.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="js/tinyfader.js"></script>
		<title>BestPC</title>
		<link rel="stylesheet" href="style/style.css" type="text/css" media="screen">
		<script type="text/javascript">
		
		  var _gaq = _gaq || [];
		  _gaq.push(['_setAccount', 'UA-22374057-1']);
		  _gaq.push(['_trackPageview']);
		
		  (function() {
		    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		  })();
		
		</script>
	</head>
	<body>		
		<div id="container">
			<div id="page_header">					
					<div id="page_heading">
						<a href="index.php">
							<h1>BestPC</h1>
							<h2> If You Can`t Find It Here, It Does Not Exist</h2>
						</a>
					</div>
			
					<div id="page_headerlinks">
							<?php
								if(!isset($_SESSION['logged'])) {
									echo '<a href="register.php">Registreer</a>';
									echo " | ";
								} else {
									echo "<a href='account.php'>Mijn Account</a>";
									echo " | ";
								}
								if(!isset($_SESSION['logged'])) {
									
								}
								echo '<a href="winkelwagen.php?action=none">Winkelwagen</a>';
								if(isset($_SESSION['logged'])) {
						          	if($_SESSION['logged']) {					
						          		echo " | ";
										echo'Welkom '.$_SESSION['real_name'];
									}
								}							
							?>
			
					</div>
					<div class="page_headersearch2">
					<?php
						if(isset($_SESSION['logged']))
						{
				          	if($_SESSION['logged'])
				          	{
				          		echo('<p>U bent ingelogd als: ' . $_SESSION['username'] . '</p>');
				          		echo('<form name="submit" method="post" action="index.php">
				          		<input class="button" type="submit" name="logout" value="Uitloggen">
				          		</form>');
				          	}
					    }
						if(!isset($_SESSION['logged']))
						{
							echo('<form name="submit" method="post" action="');?><?php echo $_SERVER['PHP_SELF'];?><?php echo('">
		      					Gebruikersnaam: <input class="login_input" type="text" name="username">
							    Wachtwoord: <input class="login_input" type="password" name="password">
		      					<input class="button_log" type="submit" name="send" value="Login">
							</form>');
						}
						
					?>
					</div>			
					<div class="page_headersearch">
						<h3>Zoek:</h3>
			
	<form action="search.php" method="get">
	<div id='search'>
						
	<input type="text" name='search'>
	<input type="image" name='Submit'  value='submit' src="images/button_go.gif" class="button">
	
	<br>
	
							<div class="clearthis">&nbsp;</div>
						</div>
						</form>
			
					</div>			
					<div class="clearthis">&nbsp;</div>
			
			</div>